
library(plyr)
library(xtable)
library(anesrake)
library(weights)
library(readstata13)

anes <- read.dta13("anes_timeseries_2016.dta")
anes <- subset(anes,anes$V160501=='1. FTF/CASI')
anes$weight <- as.numeric(as.character(anes$V160101f))

# 1 Strong Dem / 2 Not strong Dem / 3 Lean Dem / 4 Independent / 5 Lean Rep / 6 Not strong Rep / 7 Strong Rep / -8 DK -9 Refused
anes$party <- anes$V161158x
anes$party <- mapvalues(x=anes$party, from=c(-9,-8,1,2,3,4,5,6,7), to=c(NA,NA,1,2,3,4,5,6,7))
anes <-subset(anes,anes$party!=4)

# Ideology 1 Very Liberal / 2 Liberal / 3 Slightly Liberal / 4 Moderate / 5 Slightly Conservative / 6 Conservative / 7 Very Conservative
anes$ideology <- anes$V161126
anes$ideology <- mapvalues(x=anes$ideology, from=c(-9,-8,1,2,3,4,5,6,7,99), to=c(NA,NA,1,2,3,4,5,6,7,NA))

# -9 & -8 are Missing
anes$age <- as.numeric(as.character((anes$V161267)))
anes$age[which(anes$age %in% c(-9,-8))] <- NA
anes$age[which(anes$age > 17 & anes$age < 26)] <- 1
anes$age[which(anes$age > 25 & anes$age < 35)] <- 2
anes$age[which(anes$age > 34 & anes$age < 55)] <- 3
anes$age[which(anes$age > 54 & anes$age < 65)] <- 4
anes$age[which(anes$age > 64)] <- 5
anes$age <- as.factor(anes$age)

#90 & 95 & -9 Missing / 9 and under High School or Less / 10 Some College / 11 & 12 AA / 13 BA / 14 MA / 15 Profesional / 16 Doctorate
anes$education <- anes$V161270
anes$education <- mapvalues(x=anes$education,from=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,95),to=c(1,1,1,1,1,1,1,1,2,3,3,3,4,5,5,5,NA))

# -9 Missing / 1=White / 2=Black / 3=Asian / 4=Native American / 5=Hispanic / 6=Other
anes$race <- as.numeric(as.character(anes$V161310x))
anes$race <- mapvalues(x=anes$race,from=c(-9,1,2,3,4,5,6),to=c(NA,1,2,4,4,3,4))
table(anes$race)

# 1=Male / 2=Female
anes$gender <- as.factor(anes$V161002)

#####
##Survey 1
#####
d<-read.csv("survey1_updateddemos.csv")

study1 <- d
study1 <- study1[,c('ResponseId','Q1.3','Q1.4','Q1.5','Q1.6','Q1.7','Q1.8','Q1.12','Q1.13','Q1.14','Q1.15','Q1.16')]

study1$party.7pt <- NA
study1$party.7pt[which(study1$Q1.14=='Strong Democrat')] <- 1
study1$party.7pt[which(study1$Q1.14=='Not a strong Democrat')] <- 2
study1$party.7pt[which(study1$Q1.13=='Democratic Party')] <- 3
study1$party.7pt[which(study1$Q1.13=='Neither')] <- 4
study1$party.7pt[which(study1$Q1.13=='Republican Party')] <- 5
study1$party.7pt[which(study1$Q1.15=='Not a strong Republican')] <- 6
study1$party.7pt[which(study1$Q1.15=='Strong Republican')] <- 7
study1 <- subset(study1,study1$party.7pt!=4)
study1$party <- as.factor(study1$party.7pt)

study1$ideology <- mapvalues(x=as.character(study1$Q1.16), from=c('Extremely liberal','Liberal','Slightly liberal','Moderate, Middle of the road','Slightly conservative','Conservative','Extremely conservative'), to=c(1,2,3,4,5,6,7))

#Gender
study1$gender <- mapvalues(x=as.character(study1$Q1.3), from=c('Male','Female'), to=c(1,2))

#Race
study1$Q1.5 <- as.character(study1$Q1.5)
study1$Q1.5[which(study1$Q1.6=='Yes')] <- 'Hispanic'
study1$race <- mapvalues(x=study1$Q1.5,from=c('White','Black','Hispanic','Asian American','American Indian','Other'),to=c(1,2,3,4,4,4))

#Education
study1$education <- mapvalues(x=as.character(study1$Q1.7), from=c('Less than High School','High School / GED','Some College','2-year College Degree','4-year College Degree','Professional Degree (JD, MD)','Masters Degree','Doctoral Degree'), to=c(1,2,3,3,4,5,5,5))

#Age
study1$age <- mapvalues(x=as.character(study1$Q1.8),from=c('Prefer not to answer','18-25','26-34','35-54','55-64','65 or over'),to=c(NA,1,2,3,4,5))

#####
##Survey 2
#####

study2 <- read.csv('survey2_updateddemos.csv')
study2 <- subset(study2,study2$gc==1)
study2 <- subset(study2,study2$divisive.rounds.round1porder!='')

study2$party.7pt <- NA
study2$party.7pt[which(study2$Q11=='Strong Democrat')] <- 1
study2$party.7pt[which(study2$Q11=='Not a strong Democrat')] <- 2
study2$party.7pt[which(study2$Q9=='Democratic Party')] <- 3
study2$party.7pt[which(study2$Q9=='Neither')] <- 4
study2$party.7pt[which(study2$Q9=='Republican Party')] <- 5
study2$party.7pt[which(study2$Q10=='Not a strong Republican')] <- 6
study2$party.7pt[which(study2$Q10=='Strong Republican')] <- 7
study2 <- subset(study2,study2$party.7pt!=4)
study2$party <- as.factor(study2$party.7pt)

study2$ideology <- mapvalues(x=as.character(study2$Q8), from=c('Extremely liberal','Liberal','Slightly liberal','Moderate, Middle of the road','Slightly conservative','Conservative','Very conservative',"Don't know"), to=c(1,2,3,4,5,6,7,NA))

study2$race <- mapvalues(x=as.character(study2$Q3), from=c('White/Caucasian','African American','Hispanic','Asian','Native American','Pacific Islander','Other'), to=c(1,2,3,4,4,4,4))

study2$gender <- mapvalues(x=as.character(study2$Q2), from=c('Male','Female'), to=c(1,2))

study2$age <- as.numeric(as.character(study2$Q5))
study2$age[which(study2$age > 17 & study2$age < 26)] <- 1
study2$age[which(study2$age > 25 & study2$age < 35)] <- 2
study2$age[which(study2$age > 34 & study2$age < 55)] <- 3
study2$age[which(study2$age > 54 & study2$age < 65)] <- 4
study2$age[which(study2$age > 64)] <- 5
study2$age <- as.factor(study2$age)

study2$education <- mapvalues(x=as.character(study2$Q7), from=c('Less than High School','High School / GED','Some College','2-year College Degree','4-year College Degree','Professional Degree (JD, MD)','Masters Degree','Doctoral Degree'), to=c(1,2,3,3,4,5,5,5))
#ResponseId

#####
##Set Targets
#####
age.cat <- wpct(x=anes$age,weight=anes$weight)
party.cat <- wpct(x=anes$party,weight=anes$weight)
ideology.cat <- wpct(x=anes$ideology,weight=anes$weight)
education.cat <- wpct(x=anes$education,weight=anes$weight)
race.cat <- wpct(x=anes$race,weight=anes$weight)
gender.cat <- wpct(x=anes$gender,weight=anes$weight)

targets <- list(age.cat,party.cat,ideology.cat,education.cat,race.cat,gender.cat)
names(targets) <- c('age','party','ideology','education','race','gender')

#Weights for Study 1
rake.weight.object.1 <- anesrake(targets,study1,caseid=study1$ResponseId,verbose=FALSE,cap=7,choosemethod='total',nlim=7,iterate=TRUE,force1=TRUE,pctlim=.05)
save(rake.weight.object.1,file='rake.weight.object.1.RData')
updated.weight.frame.1 <- cbind.data.frame(rake.weight.object.1$caseid,rake.weight.object.1$weightvec)
names(updated.weight.frame.1) <- c('caseid','weights.nes')
save(updated.weight.frame.1,file='updated.weight.frame.1.RData')
summary(rake.weight.object.1)

#Weights for Study 2
rake.weight.object.2 <- anesrake(targets,study2,caseid=study2$ResponseId,verbose=FALSE,cap=7,choosemethod='total',nlim=7,iterate=TRUE,force1=TRUE,pctlim=.05)
save(rake.weight.object.2,file='rake.weight.object.2.RData')
updated.weight.frame.2 <- cbind.data.frame(rake.weight.object.2$caseid,rake.weight.object.2$weightvec)
names(updated.weight.frame.2) <- c('caseid','weights.nes')
save(updated.weight.frame.2,file='updated.weight.frame.2.RData')
summary(rake.weight.object.2)




####
##OLD CODE
####


save(rake.weight.object,file='rake.weight.object.RData')
updated.weight.frame <- cbind.data.frame(rake.weight.object$caseid,rake.weight.object$weightvec)
names(updated.weight.frame) <- c('caseid','weights.winterest')
save(updated.weight.frame,file='updated.weight.frame.RData')
summary(rake.weight.object)



####
##Code Up Study 2 Demographics
####
study2 <- read.csv('CombinedSurvey2.csv')
study2 <- subset(study2,study2$gc==1)
study2 <- subset(study2,study2$divisive.rounds.round1porder!='')

study2$black <- ifelse(study2$Q3=='African American',1,0)
study2$white <- ifelse(study2$Q3=='White/Caucasian',1,0)
study2$hispanic <- ifelse(study2$Q3=='Hispanic',1,0)
study2$other.race <- ifelse(study2$Q3 %in% c('Pacific Islander','Asian','Native American','Other'),1,0)
study2$non.white <- ifelse(study2$Q3=='White/Caucasian',0,1)

study2$male <- ifelse(study2$Q2=='Male',1,0)
study2$female <- ifelse(study2$male==1,0,1)

study2$democrat <- ifelse(study2$Q4=='Democrat',1,0)
study2$republican <- ifelse(study2$Q4=='Republican',1,0)
study2$independent <- ifelse(study2$Q4 %in% c('Democrat','Republican'), 0, 1)
study2 <- subset(study2,study2$independent==0)

study2$age <- as.numeric(as.character(study2$Q5))
study2$income <- as.numeric(as.character(mapvalues(x=study2$Q6,from=c("Less than 30,000","30,000 – 39,999","40,000 – 49,999","50,000 – 59,999","60,000 – 69,999","70,000 – 79,999","80,000 – 89,999","90,000 – 99,999","100,000 or more"),to=c(30000,35000,45000,55000,65000,75000,85000,95000,100000))))

study2$college <- as.numeric(as.character(mapvalues(x=study2$Q7,from=c("Less than High School","High School / GED","Some College","2-year College Degree","4-year College Degree","Masters Degree","Professional Degree (JD, MD)","Doctoral Degree"),to=c(0,0,0,0,1,1,1,1))))
study2$not.college <- ifelse(study2$college==0,1,0)

#Subset to Good Completes
####
##Combined into table
####

study1.demos <- study1[,c('black','white','hispanic','other.race','college','not.college','female','male','age','income','democrat','republican')]
study1.demos$study <- 'study1'

study2.demos <- study2[,c('black','white','hispanic','other.race','college','not.college','female','male','age','income','democrat','republican')]
study2.demos$study <- 'study2'

combined.demos <- rbind(study1.demos,study2.demos)

demo.frame <- ddply(combined.demos,.(study),summarise,black=mean(black,na.rm=TRUE),hispanic=mean(hispanic,na.rm=TRUE),white=mean(white,na.rm=TRUE),other.race=mean(other.race,na.rm=TRUE),college=mean(college,na.rm=TRUE),no.college=mean(not.college,na.rm=TRUE),female=mean(female,na.rm=TRUE),male=mean(male,na.rm=TRUE),age=mean(age,na.rm=TRUE),income=mean(income,na.rm=TRUE),democrat=mean(democrat,na.rm=TRUE),republican=mean(republican,na.rm=TRUE),sample.size=length(study))
demo.frame[,c(2:13)] <- round(demo.frame[,c(2:13)],digits=2)
demo.frame <- as.data.frame(t(demo.frame))
xtable(demo.frame)